package com.damieoa.damieoa.controller;

import com.damieoa.damieoa.pojo.Department;
import com.damieoa.damieoa.pojo.Staff;
import com.damieoa.damieoa.service.DepartmentService;
import com.damieoa.damieoa.vo.PageUtils;
import com.damieoa.damieoa.vo.SysResult;
import com.github.pagehelper.ISelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@Transactional
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    //新建部门
    @PostMapping("/add")
    public SysResult addDepartment(@RequestBody Department department) {
        departmentService.addDepartment(department);
        return SysResult.success();
    }

    //查询部门
    @GetMapping("/query")
    public SysResult queryDepartment(Department department) {
        return SysResult.success(PageUtils.startPage().doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                departmentService.queryDepartment(department);
            }
        }));
    }

    //根据部门id获取部门信息
    @GetMapping("/queryById")
    public SysResult getDepartmentById(Department department) {
        return SysResult.success(departmentService.getDepartmentById(department));
    }

    //修改部门信息
    @PutMapping("/update")
    public SysResult updateDepartment(@RequestBody Department department) {
        departmentService.updateDepartment(department);
        return SysResult.success();
    }

    //删除部门
    @DeleteMapping("/delete")
    public SysResult deleteDepartment(Department department) {
        departmentService.deleteDepartment(department);
        return SysResult.success();
    }

    //查询部门员工
    @GetMapping("/queryUser")
    public SysResult queryDepartmentStaff(Department department) {
        return SysResult.success(PageUtils.startPage().doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                departmentService.queryDepartmentStaff(department);
            }
        }));
    }

    //查询部门删除员工
    @DeleteMapping("/deleteUser")
    public SysResult deleteDepartmentStaff(Staff staff){
        departmentService.deleteDepartmentStaff(staff);
        return SysResult.success();
    }

}
